package com.chuantou.yuba.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.chuantou.yuba.R;
import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.StackManager;

public class SettingActivity extends BaseActionBarActivity implements OnClickListener {
	private UserTO mUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		mUser = AccountManager.getInstance(getApplicationContext()).getUserTO();
		if (mUser == null) {
			finish();
			return;
		}
		setupViews();
		setTitle(R.string.more);
	}

	private void setupViews() {
		TextView accountView = (TextView) findViewById(R.id.account);
		String phone = mUser.getMobile();
		String platform = mUser.getPlatform();
		if (TextUtils.isEmpty(platform)) {
			if (TextUtils.isEmpty(phone)) {
				accountView.setText(mUser.getUsername());
				accountView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			} else {
				accountView.setText(phone);
				accountView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.account_mobile, 0, 0, 0);
			}
		} else {
			if (Constants.PLATFORM_QQ.equals(platform)) {
				accountView.setText(R.string.qq_login);
				accountView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.account_qq, 0, 0, 0);
			} else if (Constants.PLATFORM_WX.equals(platform)) {
				accountView.setText(R.string.wx_login);
				accountView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.account_wx, 0, 0, 0);
			} else {
				accountView.setText(mUser.getUsername());
				accountView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			}
		}
		findViewById(R.id.change_password).setOnClickListener(this);
		findViewById(R.id.clear_cache).setOnClickListener(this);
		findViewById(R.id.black_list).setOnClickListener(this);
		findViewById(R.id.agreement).setOnClickListener(this);
		findViewById(R.id.disclaimer).setOnClickListener(this);
		findViewById(R.id.logout).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.change_password: {
			Intent intent = new Intent(SettingActivity.this, ChangePasswordActivity.class);
			startActivity(intent);
		}
			break;
		case R.id.clear_cache:
			BitmapBinder.clearCache(getApplicationContext());
			Toast.makeText(getApplicationContext(), R.string.cache_cleared, Toast.LENGTH_SHORT).show();
			break;
		case R.id.black_list: {
			Intent intent = new Intent(this, BlackListAcitivity.class);
			startActivity(intent);
		}
			break;
		case R.id.agreement: {
			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, UriHelper.getUserAgreementUrl());
			intent.putExtra(Constants.EXTRA_TITLE, getString(R.string.user_agreement));
			startActivity(intent);

		}
			break;
		case R.id.disclaimer: {
			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, UriHelper.getDisclaimerUrl());
			intent.putExtra(Constants.EXTRA_TITLE, getString(R.string.disclaimer));
			startActivity(intent);

		}
			break;
		case R.id.logout:
			YBApp.get().logout(null);
			StackManager.finishAll();
			startActivity(new Intent(this, LoginActivity.class));
			break;
		default:
			break;
		}
	}

}
